package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.PatientInformation;
import com.woniu.smallmedical.entity.ResponseResult;
import com.woniu.smallmedical.server.PatientInformationServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class PatientInformationController {
    @Autowired
    PatientInformationServer patientInformationServer;

    //增加患者
    @PostMapping("/addPatientInformation")
    public ResponseResult addPatientInformation(@RequestBody PatientInformation patientInformation) {
        System.out.println("患者信息！！！！" + patientInformation);
        PatientInformation p = new PatientInformation();
        p.setPatientname(patientInformation.getPatientname());
        p.setGender(patientInformation.getGender());
        p.setAge(patientInformation.getAge());
        p.setAddress(patientInformation.getAddress());
        p.setBirthday(patientInformation.getBirthday());
        p.setFlowstatus(1);
        p.setOperator(patientInformation.getOperator());

        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();// 获取当前时间
        p.setCreationtime(sdf.format(date));
        p.setPatientnumber(patientInformation.getPatientnumber());
        p.setPhonenumber(patientInformation.getPhonenumber());
        System.out.println("患者信息！！！！" + p);
        patientInformationServer.addPatientInformation(p);
        return new ResponseResult(null, 200, "患者信息！");
    }


    //通过患者paid查询该患者的患者表信息
    @GetMapping("/getPatByPaid/{paid}")
    public ResponseResult getPatByPaid(@PathVariable int paid) {
        PatientInformation patByPaid = patientInformationServer.getPatByPaid(paid);
        return new ResponseResult(patByPaid, 200, "通过患者id查询该患者的患者信息！");
    }
    //增加患者
    @GetMapping("/queryPatientInformation")
    public ResponseResult queryPatientInformation(String date, String search) {
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        if (null == search || "".equals(search)) {
            search = null;
        }
        if (null == date || "".equals(date)) {
            date = null;
        }

        return new ResponseResult(patientInformationServer.queryPatientInformation(date, search), 200, "患者信息！");
    }

    //增加患者
    @PostMapping("/deletePatientInformation")
    public ResponseResult deletePatientInformation(int  paid) {
        patientInformationServer.deletePatientInformation(paid);
        return new ResponseResult(null, 200, "患者信息！");
    }

    //增加患者
    @PostMapping("/updatePatientInformation")
    public ResponseResult updatePatientInformation(@RequestBody PatientInformation patientInformation) {
        patientInformationServer.updatePatientInformation(patientInformation);
        return new ResponseResult(null, 200, "患者信息！");
    }

    //通过患者的id来修改患者表的年龄，出生日期，手机号，地址信息
    @PutMapping("/updatePatByPaid/{sickid}")
    public ResponseResult updatePatByPaid(@PathVariable int sickid,
                                         @RequestBody PatientInformation patientInformation) {
        System.out.println("患者id==========="+sickid);
        System.out.println("修改信息==========="+patientInformation);
        patientInformationServer.updatePatByPaid(sickid,patientInformation);
        return new ResponseResult(null, 200, "通过患者的id来修改患者表的年龄，出生日期，手机号，地址信息！");
    }
    @GetMapping("/updatePatientInformationStatus")
    public ResponseResult updatePatientInformationStatus(Integer id) {
        patientInformationServer.updatePatientInformationStatus(id);
        return new ResponseResult(null, 200, "！");
    }

}
